package hash

import (
	"crypto/sha256"
	"encoding/hex"
	"io"
	"os"
	"path/filepath"
)

type Mysha256 struct {
}

func (allhash *Mysha256) GetBytesHash(data []byte) string {

	return ""
}
func (mysha256 *Mysha256) SetAlog([]string) {

}
func (mysha256 *Mysha256) SetN(n int) {

}
func (mysha256 *Mysha256) GetStringHash(datastr string) string {
	myhash := sha256.New()
	myhash.Write([]byte(datastr))
	bs := myhash.Sum(nil)
	return hex.EncodeToString(bs)
}
func (mysha256 *Mysha256) GetFileHash(filepath string) string {
	var hashValue string
	file, err := os.Open(filepath)
	if err != nil {
		return ""
	}
	defer file.Close()
	hash := sha256.New()
	if _, err := io.Copy(hash, file); err != nil {
		return ""
	}
	hashbyte := hash.Sum(nil)
	hashValue = hex.EncodeToString(hashbyte)

	return hashValue
}

//文件名变动，文件夹名字变动

//文件内容变动

func (mysha256 *Mysha256) GetFiledirHashName(filedirpath string) string {
	filedata := ""
	err := filepath.Walk(filedirpath, func(path string, info os.FileInfo, err error) error {
		if info == nil {
			return err
		}
		if info.IsDir() {
			//fmt.Println("文件夹",path)
			filedata += path
			return nil
		}
		//fmt.Println("文件",path)
		filedata += path
		return nil
	})
	if err != nil {
		return ""
	} else {
		myhash := sha256.New()
		myhash.Write([]byte(filedata))
		bs := myhash.Sum(nil)
		return hex.EncodeToString(bs)
	}

}
func (mysha256 *Mysha256) GetFiledirHashContent(filedirpath string) string {
	filedata := ""
	err := filepath.Walk(filedirpath, func(path string, info os.FileInfo, err error) error {
		if info == nil {
			return err
		}
		if info.IsDir() {
			return nil
		}
		//fmt.Println("文件",path)
		filedata += (mysha256.GetFileHash(path))
		return nil
	})
	if err != nil {
		return ""
	} else {
		myhash := sha256.New()
		myhash.Write([]byte(filedata))
		bs := myhash.Sum(nil)
		return hex.EncodeToString(bs)
	}
}

//7383f9172a56233b8ef658feecf0c85a292d5131028c72fa4303c8868caf5d9c
//b4e73acb415fcb8e38ddf2db9e660a331c50a5ecde26d7162a7fb7e00a9ca30c
//c9bda6fcaa4b7641941109eb0cdbd0984165849466a9854e61e0c020980504e2
//b4e73acb415fcb8e38ddf2db9e660a331c50a5ecde26d7162a7fb7e00a9ca30c
//
